package org.dromara.comm.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.A;
import org.dromara.comm.domain.AsSeekAdviceFrom;
import org.dromara.comm.domain.AsVolunteer;
import org.dromara.comm.mapper.AsVolunteerMapper;
import org.dromara.comm.service.AsSeekAdviceFromService;
import org.dromara.comm.service.AsVolunteerService;
import org.dromara.comm_lhz.util.QiniuUtil;
import org.dromara.community.entity.AsPartyBuilding;
import org.dromara.community.entity.dto.AsCommunityAnnouncementsDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 咨询志愿者控制层
 *
 * @author: 郭伟明
 * @date: 2024-07-30 16:59
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/asSeekAdviceFrom")
@Tag(name = "咨询志愿者管理")
public class AsSeekAdviceFromController {

    @Autowired
    QiniuUtil qiniuUtil;
    @Autowired
    private AsSeekAdviceFromService asSeekAdviceFromService;

    @Autowired
    private AsVolunteerService asVolunteerService;

    @Autowired
    private AsVolunteerMapper asVolunteerMapper;

    @Operation(summary = "咨询志愿者查询")
    @GetMapping("/query")
    private ResponseEntity<Object> query(Integer id){
        return new ResponseEntity<>(asSeekAdviceFromService.queryVolunteer(id), HttpStatus.OK);
    }

    @Operation(summary = "咨询志愿者消息推送")
    @PostMapping("/message")
    private void asSeekAdviceFroms(@RequestBody AsSeekAdviceFrom asSeekAdviceFrom){
        asSeekAdviceFromService.InsetAsSeekAdviceFromList(asSeekAdviceFrom);
    }

    @Operation(summary = "查询聊天")
    @GetMapping("/chatAll")
    private ResponseEntity<Object> selectAsSeekAdviceFromList(){
        return new ResponseEntity<>(asSeekAdviceFromService.selectAsSeekAdviceFromList(), HttpStatus.OK);
    }

    @Operation(summary = "咨询者添加")
    @PostMapping("/uploadAdd")
    public ResponseEntity<Map<String, Object>> uploadImageAndAddAnnouncement(
        @RequestParam(value = "file" , required = false)  MultipartFile file,
        @RequestParam(value = "name", required = false) String name,
        @RequestParam(value = "synopsis", required = false) String synopsis){

        Map<String, Object> response = new HashMap<>();

        try {
            // 验证上传的文件不为空
            // 验证文件是否为空
            if (file.isEmpty()) {
                response.put("message", "文件不能为空");
                return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
            }

            // 上传图片到七牛云存储，并获取图片URL
            // 上传图片
            String imageUrl = qiniuUtil.uploadImage(file);

            // 创建公告DTO，设置公告的各类属性
            // 创建公告DTO
            AsVolunteer asVolunteer = new AsVolunteer();
            asVolunteer.setName(name);
            asVolunteer.setAvatar(imageUrl);
            asVolunteer.setSynopsis(synopsis);

            // 保存公告信息
            // 保存公告
            asVolunteerMapper.insert(asVolunteer);

            // 构建响应，通知客户端公告创建成功，并提供图片URL
            response.put("message", "公告已成功创建");
            response.put("imageUrl", imageUrl);
            return new ResponseEntity<>(response, HttpStatus.OK);

        } catch (Exception e) {
            // 处理异常情况，构建响应，通知客户端发生错误
            response.put("message", "发生错误：" + e.getMessage());
            return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @Operation(summary = "咨询者修改")
    @PostMapping("/uploadUpdate")
    public ResponseEntity<Map<String, Object>> uploadImageAndUpdateAnnouncement(
        @RequestParam(value = "file", required = false) MultipartFile file,
        @RequestParam(value = "id") Long id,
        @RequestParam(value = "name", required = false) String name,
        @RequestParam(value = "synopsis", required = false) String synopsis) {

        Map<String, Object> response = new HashMap<>();

        try {
            // 根据ID查找党建，确认党建存在。
            AsVolunteer asVolunteer = asVolunteerMapper.selectById(id);
            if (asVolunteer == null) {
                response.put("message", "没找到");
                return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
            }

            // 如果上传了图片，进行图片上传并更新党建的图片URL。
            String imageUrl = null;
            if (file != null && !file.isEmpty()) {
                imageUrl = qiniuUtil.uploadImage(file);
                asVolunteer.setAvatar(imageUrl);  // 更新图片 URL
            }

            asVolunteer.setName(name);
            asVolunteer.setSynopsis(synopsis);
            asVolunteerMapper.updateById(asVolunteer);

            // 构建响应，通知客户端公告创建成功，并提供图片URL
            response.put("message", "公告已成功创建");
            response.put("imageUrl", imageUrl);
            return new ResponseEntity<>(response, HttpStatus.OK);

        } catch (Exception e) {
            // 处理异常情况，构建响应，通知客户端发生错误
            response.put("message", "发生错误：" + e.getMessage());
            return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
